import {ref} from "vue";
import {onLoad, onReachBottom} from "@dcloudio/uni-app";
import {yueDetailAPI} from "../../services/me";

export const useMoneyDetail = () => {
    // 明细类型
    const type = ref()
    // 是否有更多数据
    const is_more = ref(false)
    // 数据源
    const list = ref([])
    // 数据长度
    const _len = ref(0)
    // 请求参数
    const params = ref({
        page: 1,
        limit: 100,
        type: ''
    })
    // 加载数据
    onLoad(async () => {
        await handleIncomeList()
    })
    // 加载数据
    onReachBottom(async () => {
        if (_len.value < params.value.limit) {
            is_more.value = true
        } else {
            params.value.page++
            await handleIncomeList()
        }
    })
    // 判断是否有更多数据
    const handleIncomeList = async () => {
        const res = await yueDetailAPI(params.value)
        _len.value = res.result.length
        if (params.value.page === 1) {
            list.value = res.result
        } else {
            list.value = [...list.value, ...res.result]
        }
        if (_len.value < res.result.length) {
            is_more.value = true
        } else {
            is_more.value = false
        }
    }
    return {
        type, is_more, list, _len, params, handleIncomeList, onReachBottom
    }
}